package com.inlycat.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.inlycat.entity.Store;
import com.inlycat.entity.VideoAttr;
import com.inlycat.mapper.StoreMapper;
import com.inlycat.mapper.VideoAttrMapper;
import com.inlycat.service.StoreService;
import com.inlycat.utils.Constant;

/**
 * @author jarvan4dev@163.com
 * @since 2015年8月24日下午1:45:34
 * @category
 **/

@Service("storeService")
public class StoreServiceImpl implements StoreService {

	@Resource
	private StoreMapper storeMapper;

	@Resource
	private VideoAttrMapper videoAttrMapper;

	@Override
	public boolean insert(Store store) {
		boolean result = false;
		try{
			result = storeMapper.insert(store);
		}catch(NumberFormatException e){
			return false;
		}
		if (result) {
			VideoAttr videoAttr = new VideoAttr();
			videoAttr.setVideoId(store.getVideoId());
			videoAttr.setStoreCount(1);
			result = videoAttrMapper.updateSelectiveByVideoId(videoAttr);
		}
		return result;
	}

	@Override
	public boolean delete(Store store) {
		boolean result = storeMapper.delete(store);
		if (result) {
			VideoAttr videoAttr = new VideoAttr();
			videoAttr.setVideoId(store.getVideoId());
			videoAttr.setStoreCount(-1);
			result = videoAttrMapper.updateSelectiveByVideoId(videoAttr);
		}
		return result;
	}

	@Override
	public List<Store> selectByUserId(Long userId,String pageNum) {
		int curPage = 1;
		try{
			curPage = Integer.parseInt(pageNum);
		}catch(Exception e){
			return null;
		}
		PageHelper.startPage(curPage, Constant.PAGE_SIZE,false);
		List<Store> stores = storeMapper.selectByUserId(userId);
		return stores;
	}

	@Override
	public boolean hasStore(Store store) {
		boolean result = storeMapper.hasStore(store) > 0 ? true : false;
		return result;
	}

}
